<?php
/**
* Hospitality Plugin Controller
*
* This handles all AJAX requests from the User GUI.
* These methods will create an AJAX response containing JSON data. The JSON
* data is read by the JS code and rendered into interface.
*
* @access 	public
* @author 	Rob
* @copyright	ResMania 2009 all rights reserved.
* @version	1.0
* @link		http://developer.resmania.com/api
* @since  	11-2010
*/
class RM_User_HospitalityController extends RM_Controller
{
    /**
     * Returns json for the saved board type.
     *
     * @return json
     */
    public function getsavedboardJsonAction(){
        $unit_id = $this->_getParam('unit_id');

        try {
            $module = new RM_Module_HospitalityPrices();
            $data = array("success" => true, "board_type" => $module->getSavedBoardType($unit_id));
        } catch (Error $e){
            $data = array("success" => false);
        }

        return array(
            'data' => $data
        );
    }

    /**
     * Renders the accomodation picker
     */
    function accompickerAction(){
        ob_clean();
        $unitID = $this->_getParam('unit_id', null);
        $unitModel = new RM_Units();

        $unitsDAO = new RM_Units();
        $unitObj = $unitsDAO->get($unitID);

        // get the accommodation/board types available for this unit...
        $criteria = RM_Reservation_Manager::getInstance()->getCriteria();

        if (!isset($criteria)){
            echo $this->view->render('Hospitality/accompicker.phtml');
            die();
        }

        $stardateObj = new RM_Date(strtotime($criteria->start_datetime));
        $enddateObj = new RM_Date(strtotime($criteria->end_datetime));
        $periodObj = new RM_Reservation_Period($stardateObj , $enddateObj);


        // get all the boardtypes..
        $priceModel = new RM_UnitHospitalityPrices();
        $accomtypes = $priceModel->getBoardTypes($unitID, $periodObj);

        // now get all the prices for these $accomtypes

        // get the old boardtype
        $module = new RM_Module_HospitalityPrices();

        $priceSystem = RM_Environment::getInstance()->getPriceSystem();
        $persons = new RM_Reservation_Persons(array("adults"=>$criteria->adults,"children"=>$criteria->children,"infants"=>$criteria->infants));
        $information = new RM_Prices_Information($unitObj, $periodObj, $persons);

        foreach ($accomtypes as $accomtype){

            $accomtypeArray = array("k"=>'board_type',"value"=>$accomtype->board_type);

            $information = new RM_Prices_Information($unitObj, $periodObj, $persons, $accomtypeArray);
            $total = $priceSystem->getTotalUnitPrice($information);

            $data[] = array("name"=>$accomtype->board_type,"price"=>$total);

        }

        $unit = $unitModel->get($unitID);
        $this->view->unit_id = $unitID;
        $this->view->unit = $unit;
        $this->view->data = $data;
        echo $this->view->render('Hospitality/accompicker.phtml');
        die();
    }

    /**
     * Renders the persons picker
     */
    function personspickerAction(){
        ob_clean();
        $unitID = $this->_getParam('unit_id', null);
        $unitModel = new RM_Units();
        $unit = $unitModel->get($unitID);
        $this->view->unit_id = $unitID;
        $this->view->unit = $unit;
        echo $this->view->render('Hospitality/personspicker.phtml');
        die();
    }
}